const http = require('http')
const fs = require('fs')
const path = require('path')
http.createServer(function(request, response){
  console.log('request.url:', request.url)
  if(request.url==='/') {
    const html = fs.readFileSync(path.join(__dirname,'test.html'),'utf8')
    response.writeHead(200,{
      'Content-Type': 'text/html'
    })
    response.end(html)
  }

  if(request.url==='/script.js') {
    response.writeHead(200,{
      'Content-Type': 'text/javascript',//这样设置，浏览器就会把response.end()发送的内容当做js代码处理。
      'Cache-Control': 'max-age=20,public',//在响应头中设置缓存的过期时间。
    })
    response.end(fs.readFileSync(path.join(__dirname,'x.js'),'utf8'))
  }
}).listen(8888) 